cmake_minimum_required(VERSION 3.5)

project(av_player VERSION 0.1 LANGUAGES CXX)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOUIC ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
#set(CMAKE_C_STANDARD 18)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(QT_VERSION_MAJOR 6)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Gui Widgets Multimedia REQUIRED)

if(MSVC)
    # 使用 /O2 选项优化 Visual Studio 编译器
    set(CMAKE_CXX_STANDARD 20)
#    set(CMAKE_CXX_FLAGS "/O2" )
    add_compile_options(/EHsc)
else()
    # 使用 -O3 选项优化 GCC 编译器
    set(CMAKE_CXX_STANDARD 20)
    set(CMAKE_CXX_FLAGS "-Wall -O3")
endif()
qt6_add_resources(RESOURCE_FILES ${CMAKE_SOURCE_DIR}/qrc/sourceadd.qrc)

# Check if E: exists, and set the root directory to E: or D: accordingly
if(EXISTS "E:/")
  set(SDL_ROOT "E:/SDL")
  set(FFMPEG_ROOT "E:/ffmpeg")
else()
  set(SDL_ROOT "D:/SDL")
  set(FFMPEG_ROOT "D:/ffmpeg")
endif()
##SDL
find_path(inc_sdl NAMES SDL.h HINTS ${SDL_ROOT}/include/SDL2)
include_directories(${inc_sdl})
find_library(libsdl NAMES SDL2 HINTS ${SDL_ROOT}/lib)

message("-------find sdl library: "    ${libsdl})
##ffmpeg
set(inc_ffmepeg "${FFMPEG_ROOT}/include")
include_directories(${inc_ffmepeg})
link_directories("${FFMPEG_ROOT}/lib")
link_directories("${FFMPEG_ROOT}/bin")

###find_package(SDL2 REQUIRED)
###find_package(FFmpeg REQUIRED)

find_library(libavutil NAMES avutil HINTS ${FFMPEG_ROOT}/lib)
message("!!------ lib_ffmpeg  library:" ${libavutil})
find_library(libavformat NAMES avformat HINTS ${FFMPEG_ROOT}/lib)
find_library(libavdevice NAMES avdevice HINTS ${FFMPEG_ROOT}/lib)
find_library(libavfilter NAMES avfilter HINTS ${FFMPEG_ROOT}/lib)
find_library(libavcode NAMES avcodec HINTS ${FFMPEG_ROOT}/lib)

#find_path(libavcode NAMES libavcode.lib HINTS ${FFMPEG_ROOT}/lib)
message("2------ lib_ ffmpeg  library:"    ${libavcode})

find_library(libswscale NAMES swscale HINTS ${FFMPEG_ROOT}/lib)
find_library(libswresample NAMES swresample HINTS ${FFMPEG_ROOT}/lib)
find_library(libpostproc NAMES postproc HINTS ${FFMPEG_ROOT}/lib)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Model)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Model/Behavior)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Model/Structure)
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/Model/Optimization)
set(libffmpeg  ${libavfilter} ${libavformat} ${libavcode}  ${libavutil} ${libavdevice} ${libswscale}   ${libpostproc} ${libswresample})
#GLOB_RECURSE
file(GLOB_RECURSE USR_MODEL_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/Model/*.cpp)

file(GLOB_RECURSE USR_MODEL_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/Model/*.h*)


file(GLOB_RECURSE USR_Qt_MODEL_HEADER ${CMAKE_CURRENT_SOURCE_DIR}/Qt_Object/*.h*)
set(PROJECT_SOURCES
        main.cpp
        mainwindow.cpp
        mainwindow.h
        mainwindow.ui
        Qt_Object/QVideoWidget.cpp
        ${USR_Qt_MODEL_HEADER}
        ${USR_MODEL_HEADER}
        ${USR_MODEL_SOURCES}
)

#qt_add_resources(PROJECT_SOURCES ${CMAKE_SOURCE_DIR}/button.qrc)

if(${QT_VERSION_MAJOR} GREATER_EQUAL 6)
    qt_add_executable(4427_MediaMangent
        MANUAL_FINALIZATION
        ${PROJECT_SOURCES} ${RESOURCE_FILES}
    )
# Define target properties for Android with Qt 6 as:
#    set_property(TARGET 4427_MediaMangent APPEND PROPERTY QT_ANDROID_PACKAGE_SOURCE_DIR
#                 ${CMAKE_CURRENT_SOURCE_DIR}/android)
# For more information, see https://doc.qt.io/qt-6/qt-add-executable.html#target-creation
else()
    if(ANDROID)
        add_library(4427_MediaMangent SHARED
            ${PROJECT_SOURCES}
        )
# Define properties for Android with Qt 5 after find_package() calls as:
#    set(ANDROID_PACKAGE_SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/android")
    else()
        add_executable(4427_MediaMangent
            ${PROJECT_SOURCES} ${CMAKE_SOURCE_DIR}/qrc/sourceadd.qrc
        )
    endif()
endif()

target_link_directories(4427_MediaMangent PRIVATE ${inc_ffmepeg} ${inc_sdl} )
target_link_libraries(4427_MediaMangent PRIVATE Qt${QT_VERSION_MAJOR}::Widgets Qt${QT_VERSION_MAJOR}::Core Qt${QT_VERSION_MAJOR}::Multimedia ${libffmpeg} ${libsdl})

set_target_properties(4427_MediaMangent PROPERTIES
    MACOSX_BUNDLE_GUI_IDENTIFIER my.example.com
    MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION}
    MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}
    MACOSX_BUNDLE TRUE
    WIN32_EXECUTABLE TRUE
)

install(TARGETS 4427_MediaMangent
    BUNDLE DESTINATION .
    LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR})

if(QT_VERSION_MAJOR EQUAL 6)
    qt_finalize_executable(4427_MediaMangent)
endif()
